package com.dap.test.app;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.dap.test.DisconfTestConstant;
import com.dap.test.env.CreateEnvTest;
import com.dap.test.env.DeleteEnvTest;
import com.dap.test.env.getEnvByName;

/**
 * 更新环境信息
 * @author wshh-dengxiang
 *
 */
public class UpdateAppById {
	private static String sessionId;
	private static String appId;
	private static String appName="appUpdateTest_20160412"+Math.random();
	private static String desc="appUpdateTest_20160412"+Math.random();
	@Before
	public void before(){
		Client client = ClientBuilder.newClient();
		StringBuffer url = new StringBuffer();
		url.append(DisconfTestConstant.CONSOLE_REST_URI_PREFIX+"/services/sys/login?loginName=admin&password=admin");
		WebTarget target = client.target(url.toString());
		Response response = target.request().get();
		String str = response.getHeaderString("Set-Cookie");
		int m = str.indexOf("=");
		int n = str.indexOf(";");
		sessionId = str.substring(m+1, n);
	}
	@Test
	//应用Id=[空]
	public void test1() {
		String response = updateApp("","test",desc,sessionId);//yichangzhi
		Assert.assertEquals("返回状态比较", response.contains("\"status\":2000"), true);
	}
	
	@Test
	//应用Id=[错误格式] 
	public void test2() {
		String response = updateApp("sdf","test",desc,sessionId);//yichangzhi
		Assert.assertEquals("返回状态比较", response.contains("\"status\":2000"), true);
	}
	
	@Test
	//应用Id=[无效（数据库不存在该Id）]
	public void test3() {
		String response = updateApp("9999999999","test",desc,sessionId);//zhi bu cunzai
		Assert.assertEquals("返回状态比较", response.contains("\"success\":\"false\""), true);
		Assert.assertEquals("返回状态比较", response.contains("\"field\":{\"app\":\"更新应用信息不存在\"}"), true);
	}
	
	@Test 
	//应用名称=[有效],环境Id=[有效],
	public void test4() {
		CreateAppTest creteApp = new CreateAppTest();
		creteApp.createApp(appName,desc,sessionId);
		String res = new GetAppByName().getAppByName(appName,sessionId);
		appId = res.substring(res.indexOf("id")+4,res.lastIndexOf("name")-2);
		String response = updateApp(appId,"envUpdateTest_20160417","envUpdateTest_20160417",sessionId);//zhengchangzhi
		Assert.assertEquals("返回状态比较", response.contains("更新成功"), true);
		DeleteAppTest deleteApp = new DeleteAppTest();
		deleteApp.deleteAppById(appId,sessionId);
	}
	
	@Test
	//应用名称=[无效（录入的环境名称已存在）]
	public void test5() {
		CreateAppTest creteApp = new CreateAppTest();
		creteApp.createApp(appName,desc,sessionId);
		creteApp.createApp("appUpdateTest_20160418",desc,sessionId);
		String res = new GetAppByName().getAppByName(appName,sessionId);
		appId = res.substring(res.indexOf("id")+4,res.lastIndexOf("name")-2);
		String response = updateApp(appId,"appUpdateTest_20160418",desc,sessionId);
		Assert.assertEquals("返回状态比较", response.contains("\"success\":\"false\""), true);
		Assert.assertEquals("返回状态比较", response.contains("\"field\":{\"app\":\"更新应用名称已存在\"}"), true);
		String res1 = new GetAppByName().getAppByName("appUpdateTest_20160418",sessionId);
		String appId1 = res1.substring(res1.indexOf("id")+4,res1.lastIndexOf("name")-2);
		DeleteAppTest deleteApp = new DeleteAppTest();
		deleteApp.deleteAppById(appId1,sessionId);
		deleteApp.deleteAppById(appId,sessionId);
	}
	
	//环境修改
	public String updateApp(String appId,String name,String desc,String sessionId){
		Client client = ClientBuilder.newClient();
		StringBuffer url = new StringBuffer();
		url.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/appExtension/update?");
		url.append("id="+appId);
		url.append("&name="+name);
		url.append("&description="+desc);
		WebTarget target = client.target(url.toString());
		Response response = target.request().header("EX-JSESSIONID",sessionId).put(null);
		return  response.readEntity(String.class);
	}

}
